// 自定义插件 ./plugins/fullImportPlugin
import * as path from 'path'
import type { Plugin, ResolvedConfig } from 'vite'

export default function fullImportPlugin () {
    let config: ResolvedConfig
    return <Plugin>{
        name: 'fullImportElementPlus',
        async configResolved (conf) {
            config = conf
        },
        transform (code, id) {
            // 判断当前处理的是否是 _src/main.ts_
            if (id.indexOf('src/main.js')!=-1) {
                const name = 'ElementPlus'
                // 引入 ElementPlus 和 样式
                const prepend = `import ${name} from 'element-plus';\nimport 'element-plus/theme-chalk/src/index.scss';\n`
                code = code.replace('.mount(', ($1) => `.use(${name})` + $1)
                return prepend + code
            }
            return code
        }
    }
}
